﻿using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;

namespace TripleGame.GUI
{
    public partial class CustomControlBoard : Control
    {
        private const int DEFAULT_COLUMNS_COUNT = 10;
        private const int DEFAULT_ROWS_COUNT = 5;
        private const int CELL_SIZE = 32;

        private int rowsCount;
        private int columnsCount;
        private char[,] board;

        [Category("Behavior"), Description("Specifies the board rows count.")]
        public int RowsCount
        {
            get
            {
                return this.rowsCount;
            }
            set
            {
                this.rowsCount = value;
                this.Height = CELL_SIZE * rowsCount;
                InitializeBoard();
            }
        }

        [Category("Behavior"), Description("Specifies the board columns count.")]
        public int ColumnsCount
        {
            get
            {
                return this.columnsCount;
            }
            set
            {
                this.columnsCount = value;
                this.Width = CELL_SIZE * columnsCount;
                InitializeBoard();
            }
        }

        [Category("Behavior"), Description("Specifies the board contents.")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public char[,] Board
        {
            get
            {
                return this.board;
            }
            set
            {
                this.board = value;
                this.rowsCount = this.board.GetLength(0);
                this.columnsCount = this.board.GetLength(1);
                this.Invalidate();
            }
        }

        private void InitializeBoard()
        {
            this.board = new char[this.rowsCount, this.columnsCount];
            for (int row = 0; row < this.rowsCount; row++)
            {
                for (int col = 0; col < this.columnsCount; col++)
                {
                    this.board[row, col] = ' ';
                }
            }
            this.Invalidate();
        }

        public CustomControlBoard()
        {
            InitializeComponent();
            this.ColumnsCount = DEFAULT_COLUMNS_COUNT;
            this.RowsCount = DEFAULT_ROWS_COUNT;
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);

            Graphics g = pe.Graphics;
            for (int row = 0; row < this.rowsCount; row++)
            {
                for (int col = 0; col < this.columnsCount; col++)
                {
                    if (this.DesignMode)
                    {
                        g.DrawRectangle(Pens.Black, col * CELL_SIZE, row * CELL_SIZE,
                            CELL_SIZE - 1, CELL_SIZE - 1);
                    }
                    else
                    {
                        char cellChar = this.board[row, col];
                        Image img = ImageUtils.GetImage(cellChar);
                        g.DrawImage(img, col * CELL_SIZE, row * CELL_SIZE);
                    }
                }
            }
        }
    }
}